<?php
$title='相片管理';
define('IN_TG', 'admin_photo');
include('admin_header.php');
//判断是否有传id值到该页面
if(isset($_GET['id']))
{
    //判断是否存在p参数，存在则使用传递来的页码，不存在则使用默认值1
    $sid=$_GET['id'];
    $page=isset($_GET['p'])?$_GET['p']:1;
    $sql = "select tg_id,tg_name,tg_url,tg_username,tg_readcount,tg_commendcount,tg_date,tg_active from tg_photo where tg_sid={$sid} order by tg_date limit ".intval(($page-1)*5).",5";
    $res = _query($sql);
    $sql="select tg_id from tg_photo where tg_sid={$sid}";
    $total=_num_rows(_query($sql));
    $page_total=ceil($total/5);
}
else
{
    _location('请不要非法操作','admin_dir.php');
}
?>
<div class="row">
    <div class="col-lg-12">
        <h1>多用户留言 <small>后台管理系统</small></h1>
        <ol class="breadcrumb">
            <li><a href="admin_index.php"><i class="fa fa-home"></i> 首页</a></li>
            <li><a href="admin_dir.php"><i class="fa fa-home"></i> 相册管理 </a></li>
            <li class="active"><i class="fa fa-table"></i> <?php echo $title;?></li>
        </ol>
        <div class="alert alert-danger alert-dismissable" id="photo_warming_alert" style="display: none">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
            <span id="photo_warming"></span>
        </div>
    </div>
</div><!-- /.row -->

<div class="row">
    <div class="col-lg-12">
        <div class="table-responsive">
            <table class="table table-bordered table-hover tablesorter">
                <thead>
                <tr>
                    <th>ID <i class="fa fa-sort"></i></th>
                    <th>相片名 <i class="fa fa-sort"></i></th>
                    <th>相片地址 <i class="fa fa-sort"></i></th>
                    <th>相片上传者 <i class="fa fa-sort"></i></th>
                    <th>相片阅读数 <i class="fa fa-sort"></i></th>
                    <th>相片评论量 <i class="fa fa-sort"></i></th>
                    <th>相片上传时间 <i class="fa fa-sort"></i></th>
                    <th>相片操作 </th>
                </tr>
                </thead>
                <tbody>
                <?php while($row = _fetch_array_list($res)): ?>
                    <tr>
                        <td><?php echo $row['tg_id'];?></td>
                        <td><?php echo $row['tg_name'];?></td>
                        <td><img src="<?php echo $row['tg_url']==''?'images/photo.jpg':$row['tg_url'];?>" title="<?php echo $row['tg_name'];?>" alt="<?php echo $row['tg_name'];?>" height="70"></td>
                        <td><?php echo $row['tg_username'];?></td>
                        <td><?php echo $row['tg_readcount'];?></td>
                        <td><?php echo $row['tg_commendcount'];?></td>
                        <td><?php echo $row['tg_date'];?></td>
                        <td class="text-center">
                            <?php if($row['tg_active']==1):?>
                                <button type="button" class="btn btn-danger photo_active" data-id="<?php echo $row['tg_id'];?>" data-active="<?php echo 1-$row['tg_active'];?>">禁用</button>
                            <?php else:?>
                                <button type="button" class="btn btn-success photo_active" data-id="<?php echo $row['tg_id'];?>" data-active="<?php echo 1-$row['tg_active'];?>">启用</button>
                            <?php endif;?>
                        </td>
                    </tr>
                <?php endwhile;?>
                </tbody>
            </table>
            <div class="text-right">
                <ul class="pagination pagination-lg">
                    <li <?php if($page==1):?>class="disabled" <?php endif;?>><a href="?id=<?php echo $sid;?>&&p=1">«</a></li>
                    <?php for($i=1;$i<=$page_total;$i++)
                    {
                        if ($page == $i) {
                            echo '  <li class="active"><a href="?id='.$sid.'&&p='.$i.'">'.$i.'</a></li>';
                        } else {
                            echo '  <li><a href="?id='.$sid.'&&p='.$i.'">'.$i.'</a></li>';
                        }
                    }
                    ?>
                    <li <?php if($page==$page_total):?>class="disabled" <?php endif;?>><a href="?id=<?php echo $sid;?>&&p=<?php echo $page_total;?>">»</a></li>
                </ul>
            </div>
        </div>
    </div>
</div><!-- /.row -->



</div><!-- /#page-wrapper -->
<?php include('admin_footer.php');?>
<script>
    $(function(){
        $("#admin_photo").attr('class','active');
        $(".photo_active").click(function(){
            //默认隐藏错误提示框
            $("#photo_warming_alert").hide();
            //取得用户id
            var photo_id=$(this).data('id');
            //取得用户激活状态
            var photo_active=$(this).data('active');
            var this_item=$(this);
            $.ajax({
                type:'post',
                url:'admin_action.php',
                data:{type:'photo_active_change',photo_id:photo_id,photo_active:photo_active},
                dataType:'json',
                success:function(data)
                {
                    if(data.status==1)
                    {
                        //激活用户
                        if(photo_active==1)
                        {
                            //设置禁用
                            this_item.attr('class','btn btn-danger user_active');
                            this_item.html('禁用');
                        }
                        else
                        {
                            //禁用用户设置激活
                            this_item.attr('class','btn btn-success user_active');
                            this_item.html('启用');
                        }
                        //修改激活状态
                        this_item.data('active',1-photo_active);
                    }
                    else {
                        //显示错误信息
                        $("#photo_warming").html(data.msg);
                        $("#photo_warming_alert").show();
                    }
                },
                error:function()
                {
                    //异常显示
                    $("#photo_warming").html('网络异常');
                    $("#photo_warming_alert").show();
                }
            })
        })
    })
</script>